package scr 
{
	import cell.KeyBoard;
	import cell.KeyPad;
	import flash.events.Event;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import img.ScalePlane;
	import ru.gosoplata.model.TmpModel;
	import uicontrols.ButtonBase;
	/**
	 * TODO: ScreenSearch - popup
	 * @author a.orleansky
	 */
	public class ScreenSearch extends ScreenBase implements IScreen
	{
		public var keyboard:KeyBoard;

		public var btn_back:ButtonBase;
		public var btn_next:ButtonBase;
		private var pic_count:Number;
		private var input_plane:ScalePlane;
		private var up_txt:TextField;
		
		private var	up_txt_str:String = "Введите название провайдера";
		
		public function ScreenSearch() 
		{
			super("search");
		}
		
		override protected function init(e:Event):void 
		{
			super.init(e);
			
			graphics.beginFill(0xF5F5F5, 0.9);
			graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			graphics.endFill();
			pic_count=0;
			keyboard = new KeyBoard();
			keyboard.x = 50;
			keyboard.y = 400;
			keyboard.num_limit = 20;
			keyboard.clearEnteredValue();
			addChild(keyboard);
			input_plane = new ScalePlane(910);
			input_plane.x = 50;
			input_plane.y = keyboard.y - input_plane.height - 40;
			addChild(input_plane);
			createBtns();
			keyboard.addEventListener(KeyPad.KEY_PAD_RELEASED, update);
			
			var txt_type3:TextFormat = TmpModel.getTextFormat(38, 0x676767,true);
			txt_type3.letterSpacing = 1;
			
			up_txt = new TextField();
			up_txt.embedFonts = true;
			up_txt.y = input_plane.y - 60;
			up_txt.x = input_plane.x;
			up_txt.width = 800;
			up_txt.defaultTextFormat = txt_type3;
			up_txt.antiAliasType = AntiAliasType.ADVANCED;
			up_txt.thickness = -50;
			up_txt.height = 140;
			up_txt.multiline = true;
			up_txt.text = up_txt_str;
			up_txt.selectable = false;
			addChild(up_txt);
		}	
		
		private function update(e:Event):void 
		{
			if((e.target as KeyBoard).entered_value.length <= 10){
				//TmpModel.userInfo.search = (e.target as KeyBoard).entered_value;
				//input_plane.text = TmpModel.userInfo.search;
				input_plane.text = (e.target as KeyBoard).entered_value;
			}
		}
		
		override public function reDraw():void
		{ 
			keyboard.clearEnteredValue();
			//TmpModel.userInfo.search = "";
			input_plane.text = "";
		}
		
		private function createBtns():void 
		{
			btn_back = new ButtonBase("exit", 0xFF0000);
			btn_back.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_back.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			addChild(btn_back);
			pic_count++
			
			btn_next = new ButtonBase("search", 0x00FF00);
			btn_next.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_next.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			addChild(btn_next);
			pic_count++;
		}		
		
		private function allPicLoaded(e:Event):void 
		{
			pic_count--;
			if (pic_count == 0) {
				btn_next.x = stage.stageWidth - (btn_next.width + 20)
				btn_next.y = input_plane.y;
				
				btn_back.x = stage.stageWidth - (btn_back.width + 20)
				btn_back.y = stage.stageHeight - (btn_back.height + 20);
			}
		}	
		
		private function pressBtn(e:Event):void {
			switch(e.target.name) {
				case "exit":
					TmpModel.tmpGlobalController.onClick("back");
					break;
				case "search":
					TmpModel.tmpGlobalController.controllerSerchProvider(input_plane.getText());
					break;
			}
		}
		
		
	}

}